/* 
 * Copyright 2013 Sébastien Mouquet.
 * 
 * This file is part of ECovoiturage.
 * 
 * ECovoiturage is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ECovoiturage is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Foobar. If not, see http://www.gnu.org/licenses/.
 */
package com.smouquet.ecovoiturage.ui.page.root;

import com.smouquet.ecovoiturage.ApplicationContext;
import com.smouquet.ecovoiturage.core.bean.TrajetMidiItem;
import com.smouquet.ecovoiturage.core.event.TrajetMidiSelectedEvent;
import com.smouquet.ecovoiturage.core.service.TrajetService;
import com.smouquet.ecovoiturage.ui.page.root.TrajetMidiTable.SelectedItemChangedEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.themes.Runo;
import java.util.List;
import org.vaadin.addon.borderlayout.BorderLayout;

/**
 * Vue correspondant à la liste des trajets du midi.
 * 
 * @author sebastien.mouquet
 */
public class TrajetMidiListView extends BorderLayout
                                implements View, TrajetMidiTable.SelectedItemChangedListener
{
    public static final String VIEW_NAME = "trajetmidi/list";
    
    private TrajetMidiTable table;
    
    public TrajetMidiListView()
    {
        
    }
    

    @Override
    public void enter(ViewChangeEvent event)
    {
        TrajetService service = ApplicationContext.getInstance(TrajetService.class);
        List<TrajetMidiItem> trajets = service.getAllTrajetsMidiByJour(null);
        
        table = new TrajetMidiTable(trajets);
        table.setSizeFull();
        table.addStyleName(Runo.TABLE_BORDERLESS);
        table.addSelectedItemChangedListener(this);
        
        addComponent(table, BorderLayout.Constraint.CENTER);
        
        setSizeFull();
    }

    
    @Override
    public void onSelectedItemChanged(SelectedItemChangedEvent event)
    {
        ((RootUI)getUI()).getEventBus().post(new TrajetMidiSelectedEvent(event.getItem()));
    }
}
